package cn.yuanqiao.archive.appManager.service.impl;

import cn.yuanqiao.archive.appManager.domain.TenantSysDictData;
import cn.yuanqiao.archive.appManager.mapper.TenantSysDictDataMapper;
import cn.yuanqiao.archive.appManager.service.ITenantSysDictDataService;
import cn.yuanqiao.archive.appManager.utils.TenantYqdaDictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zjj
 * @date 2023/6/21
 */
@Service
public class TenantSysDictDataServiceImpl implements ITenantSysDictDataService {
    @Autowired
    private TenantSysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<TenantSysDictData> selectDictDataList(TenantSysDictData dictData)
    {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue,new TenantSysDictData().getTableName());
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public TenantSysDictData selectDictDataById(Long dictCode)
    {
        return dictDataMapper.selectDictDataById(dictCode,new TenantSysDictData().getTableName());
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes)
    {
        for (Long dictCode : dictCodes)
        {
            TenantSysDictData data = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode,data.getTableName());
            List<TenantSysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType(),data.getTableName());
            TenantYqdaDictUtils.setDictCache(data.getDictType(), dictDatas);
        }
    }

    /**
     * 新增保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(TenantSysDictData data)
    {
        int row = dictDataMapper.insertDictData(data);
        if (row > 0)
        {
            List<TenantSysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType(),data.getTableName());
            TenantYqdaDictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(TenantSysDictData data)
    {
        int row = dictDataMapper.updateDictData(data);
        if (row > 0)
        {
            List<TenantSysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType(),data.getTableName());
            TenantYqdaDictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }
}
